package com.cysyz.mylib.utils;


import com.cysyz.mylib.stringconverter.StringConverter;
import com.cysyz.mylib.stringconverter.impl.Str2DoubleConverter;
import com.cysyz.mylib.stringconverter.impl.Str2IntConverter;
import com.cysyz.mylib.utils.myassert.MyAssert;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class MyStringUtils extends StringUtils{
    private static List<StringConverter> stringConverters;

    static {
        stringConverters = new ArrayList<StringConverter>();
        stringConverters.add(new Str2IntConverter());
        stringConverters.add(new Str2DoubleConverter());
    }

    public static String firstChar2UpperCase(String str) {
        MyAssert.notNull(str);

        str  = str.substring(0,1).toUpperCase()+str.substring(1);

        return str;
    }

    public static String getSetter(String propertyName) {
        MyAssert.notNull(propertyName);

        return "set" + firstChar2UpperCase(propertyName);
    }
    public static String getGetter(String propertyName) {
        MyAssert.notNull(propertyName);

        return "get" + firstChar2UpperCase(propertyName);

    }

    public static String listToCsv(List<String> list) {
        StringBuilder csv = null;
        if (list != null) {
            csv = new StringBuilder();
            for (String phoneNum : list) {
                csv.append(phoneNum).append(",");
            }
            csv.deleteCharAt(csv.length() - 1);
        }
        return csv == null ? null : csv.toString();
    }

    public static <T> List<T> csvToList(String csv, Class<T> valType) {
        StringConverter stringConverter = null;
        for (StringConverter _stringConverter : stringConverters) {
            if (_stringConverter.support(valType)) {
                stringConverter = _stringConverter;
            }
        }
        String[] split = csv.split(",");
        List<T> result = new ArrayList<T>();
        for (String str : split) {
            result.add((T) stringConverter.convert(str.trim()));
        }
        return result;
    }

    public static char lastChar(String str) {
        return str.charAt(str.length() - 1);
    }

    public static StringBuilder deleteLastChar(StringBuilder str) {
        return str.deleteCharAt(str.length() - 1);
    }










    /**
     * 字符串格式化工具
     */
    public static class Format {
        public static String toAbcd(String inputValue) {
            MyAssert.notNull(inputValue);
            StringBuilder returnValue = new StringBuilder();
            returnValue.append(inputValue.substring(0, 1).toUpperCase()).append(inputValue.substring(1).toLowerCase());
            return returnValue.toString();
        }

        public static String AB_AB2abAb(String inputValue) {
            MyAssert.notNull(inputValue);
            String[] words = inputValue.split("_");
            StringBuilder returnValue = new StringBuilder(words[0].toLowerCase());
            for (int i = 1 ; i < words.length ; i++) {
                returnValue.append(MyStringFormatUtils.toAbcd(words[i]));
            }
            return returnValue.toString();
        }
    }

}
